package com.iotechn.unimall.admin.api.warehouse;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.domain.WarehouseDO;
//import com.iotechn.unimall.data.enums.IsMainWarehouseType;
import com.iotechn.unimall.data.mapper.WarehouseMapper;
import com.iotechn.unimall.data.model.Page;
import com.iotechn.unimall.data.util.SessionUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


@Service
public class AdminWarehouseServiceImpl implements AdminWarehouseService {

    @Autowired
    private WarehouseMapper warehouseMapper;

    @Override
    public Page<WarehouseDO> list(String warehouseName, String warehouseContacts, String warehousePhone, Integer isMain, Integer status, Integer page, Integer limit, Long adminId) throws ServiceException {
        Wrapper<WarehouseDO> wrapper = new EntityWrapper<WarehouseDO>();
        if (!StringUtils.isEmpty(warehouseName)) {
            wrapper.like("warehouse_name", warehouseName);
        }
        if (!StringUtils.isEmpty(warehouseContacts)) {
            wrapper.like("warehouse_contacts", warehouseContacts);
        }
        if (!StringUtils.isEmpty(warehousePhone)) {
            wrapper.like("warehouse_phone", warehousePhone);
        }
        if (status != null) {
            wrapper.eq("status", status);
        }
        if (isMain != null) {
            wrapper.eq("is_main", isMain);
        }
        wrapper.orderBy("id", false);
        Integer count = warehouseMapper.selectCount(wrapper);
        List<WarehouseDO> WarehouseDOS = warehouseMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        return new Page<>(WarehouseDOS, page, limit, count);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public WarehouseDO create(WarehouseDO warehouseDO, Long adminId) throws ServiceException {
        warehouseDO.setAdminId(adminId);
        Date now = new Date();
        warehouseDO.setGmtCreate(now);
        warehouseDO.setGmtUpdate(now);
        warehouseDO.setStatus(1);//创建时状态为1
        this.handleMainWarehouse(warehouseDO.getId(), warehouseDO.getIsMain());
        if (warehouseMapper.insert(warehouseDO) > 0) {
            return warehouseDO;
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String update(WarehouseDO warehouseDO, Long adminId) throws ServiceException {
        Long id = warehouseDO.getId();
        if (id == null) {
            return "fail";
        }
        Date now = new Date();
        warehouseDO.setGmtUpdate(now);
        this.handleMainWarehouse(warehouseDO.getId(), warehouseDO.getIsMain());
        if (warehouseMapper.updateById(warehouseDO) > 0) {
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delete(Long id, Long adminId) throws ServiceException {
        if (warehouseMapper.deleteById(id) > 0) {
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    public WarehouseDO detail(Long id, Long adminId) throws ServiceException {
        return warehouseMapper.selectById(id);
    }

    @Override
    public String updateStatus(Long id, Integer status, Long adminId) throws ServiceException {
        WarehouseDO warehouseDO = warehouseMapper.selectById(id);
        if (warehouseDO == null) {
            return "fail";
        }
        warehouseDO.setStatus(status);//冻结
        Date now = new Date();
        warehouseDO.setGmtUpdate(now);
        warehouseDO.setAdminId(adminId);


        if (warehouseMapper.updateById(warehouseDO) > 0) {//删除改为假删除，更改状态为0
            return "ok";
        }
        return "fail";
    }

    @Override
    public List<WarehouseDO> listWareHousesByPara(String status,Integer isMain, Long adminId) throws ServiceException {
        Long id = SessionUtil.getAdmin().getId();
        List<WarehouseDO> warehouseDOList = warehouseMapper.selectList(new EntityWrapper<WarehouseDO>()
                .eq(!StringUtils.isEmpty(status), "status", status)
        .eq(isMain!=null,"is_main",isMain));
       // .eq("admin_id",id));
        return warehouseDOList;
    }

    //处理主仓库
    private void handleMainWarehouse(Long warehouseId, Integer isMainWarehouse) {
        /*if (isMainWarehouse != null && isMainWarehouse == IsMainWarehouseType.IS_MAIN_WAREHOUSE.getCode()) {
            WarehouseDO updateWarehouseDo = new WarehouseDO();
            updateWarehouseDo.setIsMain(IsMainWarehouseType.IS_NOT_MAIN_WAREHOUSE.getCode());//主仓库
            updateWarehouseDo.setGmtUpdate(new Date());
            warehouseMapper.update(updateWarehouseDo, new EntityWrapper<WarehouseDO>()
                    .ne(warehouseId != null, "id", warehouseId)
                    .eq("is_main", IsMainWarehouseType.IS_MAIN_WAREHOUSE.getCode()));
        }*/
    }
}